﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exam
{
    public partial class AddUpdate : Form
    {
        public AddUpdate()
        {
            InitializeComponent();
        }
        private int id;

       public AddUpdate(int id,string name,int age,int score)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = name;
            textBox2.Text = age.ToString();
            textBox3.Text = score.ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var age = textBox2.Text;
            var score = textBox3.Text;

          if (this.id > 0)//更新
            {
                var sql = string.Format("update Students set Name='{0}', Age = {1},Score={2} where Id = {3}", name, age, score, this.id);

                DbHelper.AddOrUpdate(sql);

                MessageBox.Show("更新成功!", "提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else//添加
            {
                var sql = string.Format("insert into Students(Name,Age,Score)values('{0}',{1},{2})",name,age,score);

                DbHelper.AddOrUpdate(sql);

                MessageBox.Show("添加成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.Yes ;
            this.Close();
           

        }
      
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox2.Focus();

            }
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox3.Focus();

            }
        }
    }
}
